<template>
  <TopHeader />
  <BottomNavigation />
  <div class="login-container">
    <div class="login-wrapper">
      <div class="toggle-bar">
        <span :class="{ active: isLogin }" @click="isLogin = true">SIGN IN</span>
        <span :class="{ active: !isLogin }" @click="isLogin = false">SIGN UP</span>
        <div class="toggle-switch" :class="{ right: !isLogin }"></div>
      </div>
      <h1 v-if="isLogin">医疗系统登录</h1>
      <h1 v-else>医疗系统注册</h1>
      <form v-if="isLogin" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名：</label>
          <input 
            type="text" 
            id="username" 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            required
            autocomplete="username"
          >
        </div>
        <div class="form-group">
          <label for="password">密码：</label>
          <input 
            type="password" 
            id="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            required
            autocomplete="current-password"
          >
        </div>
        <button 
          type="submit" 
          :disabled="loading"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
      <form v-else @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="reg-username">用户名：</label>
          <input 
            type="text" 
            id="reg-username" 
            v-model="registerForm.username" 
            placeholder="请输入用户名"
            required
            autocomplete="username"
          >
        </div>
        <div class="form-group">
          <label for="reg-realname">真实姓名：</label>
          <input 
            type="text" 
            id="reg-realname" 
            v-model="registerForm.realName" 
            placeholder="请输入真实姓名"
            required
            autocomplete="name"
          >
        </div>
        <div class="form-group">
          <label for="reg-password">密码：</label>
          <input 
            type="password" 
            id="reg-password" 
            v-model="registerForm.password" 
            placeholder="请输入密码"
            required
            autocomplete="new-password"
          >
        </div>
        <div class="form-group">
          <label for="reg-confirm">确认密码：</label>
          <input 
            type="password" 
            id="reg-confirm" 
            v-model="registerForm.confirmPassword" 
            placeholder="请再次输入密码"
            required
            autocomplete="new-password"
          >
        </div>
        <button 
          type="submit" 
          :disabled="loading"
        >
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import TopHeader from '@/components/TopHeader.vue'
import { useUserStore } from '@/stores/user' 
import BottomNavigation from '@/components/BottomNavigation.vue';

const router = useRouter();
const loading = ref(false);
const isLogin = ref(true);

const userStore = useUserStore()
const loginForm = reactive({
  username: '',
  password: ''
});
const registerForm = reactive({
  username: '',
  realName: '',
  phone: '',
  password: '',
  confirmPassword: ''
});

const handleLogin = async () => {
  loading.value = true;
  try {
    const response = await axios.post(
      'http://localhost:8080/hospital5_war/user/login',
      {
        userName: loginForm.username,
        password: loginForm.password
      },
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );
    userStore.setUserInfo(response.data.userInfo);
    router.push('/yonghu/online-registration');
  } catch (error) {
    alert('登录失败，请检查网络或用户名密码');
  } finally {
    loading.value = false;
  }
};

const handleRegister = async () => {
  loading.value = true;
  try {
    const response = await axios.post(
      'http://localhost:8080/hospital5_war/user/addUser',
      {
        userName: registerForm.username,
        realName: registerForm.realName,
        phone: registerForm.phone,
        password: registerForm.password,
        userRole: 'user' 
      },
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );
    if (response.data.success) {
      alert('注册成功，请登录');
      isLogin.value = true;
      registerForm.username = '';
      registerForm.realName = '';
      registerForm.password = '';
      registerForm.confirmPassword = '';
    } else {
      alert(response.data.message || '注册失败');
    }
  } catch (error) {
    alert('注册失败，请检查网络或用户名是否已存在');
  } finally {
    loading.value = false;
  }
};
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #e6f7ff 0%, #b3e0ff 100%);
}

.login-wrapper {
  background-color: #ffffff;
  padding: 32px 32px 24px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 120, 215, 0.15);
  width: 360px;
  position: relative;
}

.toggle-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  position: relative;
  height: 40px;
  width: 220px;
  margin-left: auto;
  margin-right: auto;
  background: #f0f8ff;
  border-radius: 20px;
}

.toggle-bar span {
  color: #5a8cce;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.2s;
  z-index: 2;
  width: 100px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.toggle-bar span.active {
  color: #ffffff;
  font-weight: 600;
}

.toggle-switch {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 100px;
  height: 34px;
  background: #1890ff;
  border-radius: 17px;
  transition: left 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
  z-index: 1;
}

.toggle-switch.right {
  left: 120px;
}

h1 {
  text-align: center;
  margin-bottom: 20px;
  color: #1890ff;
  font-size: 2rem;
  font-weight: bold;
}

.form-group {
  margin-bottom: 18px;
}

label {
  display: block;
  margin-bottom: 5px;
  color: #333;
  font-size: 1.1rem;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #cce6ff;
  border-radius: 8px;
  background: #f7fbff;
  color: #333;
  font-size: 1rem;
  transition: border-color 0.3s;
}

input:focus {
  border-color: #1890ff;
  outline: none;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

button {
  width: 107%;
  padding: 14px;
  background-color: #1890ff;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  font-size: 1.2rem;
  margin-top: 18px;
  letter-spacing: 2px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #40a9ff;
}

button:active {
  background-color: #096dd9;
}

button:disabled {
  background-color: #91d5ff;
  cursor: not-allowed;
}
</style>